﻿using System.Web.Mvc;
using BSU.GraduateWork.Data.Model;
using BSU.GraduateWork.Utilities;
using BSU.GraduateWork.Web.Models;

namespace BSU.GraduateWork.Web.Binders
{
    public class SolutionModelBinder: BaseModelBinder
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            SolutionModel model = bindingContext.Model as SolutionModel ?? new SolutionModel(new Problem3D());
            var form = controllerContext.HttpContext.Request.Form;

            var inputParameters = new InputParameters3D();

            inputParameters.N = ParseParamAsInt(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.N)]);

            inputParameters.X1 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.X1)]);
            inputParameters.X2 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.X2)]);
            inputParameters.Y1 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Y1)]);
            inputParameters.Y2 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Y2)]);
            inputParameters.Z1 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Z1)]);
            inputParameters.Z2 = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Z2)]);

            inputParameters.G = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.G)]);
            inputParameters.H = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.H)]);
            inputParameters.Alpha = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Alpha)]);
            inputParameters.Epsilon = ParseParamAsDouble(form[Reflector.GetFullPropertyName<SolutionModel>(x => x.Problem.InputParameters.Epsilon)]);

            model.Problem.InputParameters = inputParameters;

            return model;
        }
    }
}
